//CollisionManager.h
//Created 16/05/14
//Created By Dan & Harry 
//
//Class which will handle collisions within our game - Essentially returns
//true or false for each test.

#pragma once

//Include XNA maths.
#include "d3dUtil.h"

//Used to perform collision detection tests. 
#include "xnacollision.h"

//If uncommented, the developer will be allowed to debug
//draw the AABB's. The class will create a unit box.
#define SHOULD_ALLOC_AABB_RENDERING_DATA

#ifdef SHOULD_ALLOC_AABB_RENDERING_DATA
//Colour of the AABB box. 
#define BOX_DEBUG_COLOUR XMFLOAT3(0.0f, 0.0f, 0.3f)

//Forward declare effect used to debug draw box
class TileForwardSolidColourFX;
//Forward declare camera - needed when rendering
class VirtualCamera;
#endif

//Structure represeniting our custom AABB. 
struct AABB
{
	AABB()
	{

	}

	AABB(XMFLOAT3 center, XMFLOAT3 extents)
	{
		CenterW = center;
		ExtentsW = extents;
	}

	//World space center location of the bounding box
	XMFLOAT3 CenterW;
	//World space size of the box. 
	XMFLOAT3 ExtentsW;
};

class CollisionManager
{
public:
	//Returns an instance of the collision manager
	static CollisionManager* GetInstance();

	//Destroys shared instance - managed by Application.h/cpp
	static void Release();

public:
	//Returns the (unsigned) distance between two WORLD space locations
	float CalculateUnsignedDistance(XMFLOAT3 a, XMFLOAT3 b);

	//Does collision detection with our own calculated World space AABB. Returns
	//true if a collision is detected and thus you should respond. 
	bool TestCollision(AABB* a, AABB* b);

#ifdef SHOULD_ALLOC_AABB_RENDERING_DATA
public:
	//Debug draws a bounding box
	void DebugDrawAABB(AABB* box, VirtualCamera* camera, XMFLOAT3 colour = BOX_DEBUG_COLOUR);
#endif

private:
	//Hide methods to construct/delete 
	CollisionManager();
	~CollisionManager();
	CollisionManager(CollisionManager& other);
	CollisionManager& operator=(CollisionManager& other);

	//Shared singleton instance
	static CollisionManager* instance;

	//Only if required...
#ifdef SHOULD_ALLOC_AABB_RENDERING_DATA
private:
	//Load box VB and IB - 
	void LoadBoxVertexData();

	//VB and IB of the unit box
	ID3D11Buffer* boxVB;
	ID3D11Buffer* boxIB;

	//Box num indicies to draw it.
	unsigned boxIndexCount;

	//Effect used to render box
	TileForwardSolidColourFX* debugFX;
#endif
};